var Player = require("../../domain/player")
var skillAttack = require("../../domain/skillAttack")
var redisClient = require('../../dao/redis/redisClient')
var playerCon = require('../../domain/playerCon')
var Bag = require("../../domain/bag")
var Equipment = require("../../domain/equipment")
var utils = require('../../util/utils')
module.exports = function(app) {
    app.all('/service/*', function(req, res, next) {
        //是否选择角色
        var playerid = req.session.playerid
        if (playerid) {
            var player = global.playerTree.get(playerid)
            if (player) {
                req.player = player
                next()
            } else {
                //获取角色属性
                redisClient.get('p:' + playerid, function(err, redisPlayer) {
                    player = JSON.parse(redisPlayer)
                    player = new Player(player)
                        //获取角色物品信息,背包
                    redisClient.mget('i:' + playerid, 'b:' + playerid, function(err, ret) {
                        if (ret[0]) {
                            //物品信息
                            player.itemInfo = JSON.parse(ret[0])
                        } else {
                            player.itemInfo = {}
                        }
                        if (ret[1]) {
                            //背包
                            player.bag = new Bag(JSON.parse(ret[1]))
                        } else {
                            player.bag = new Bag({})
                        }
                        global.playerTree.put(playerid, player)
                        player.calcAttri()
                        req.player = player
                        next()
                    })
                })
            }
        } else {
            res.send({
                error: 102
            })
            return
        }
    })

    //查看商品
    app.post('/service/merchant', function(req, res) {
        var merchant = global.merchant
        res.send({
            error: 0,
            merchant: merchant
        })
    })

    //购买商品
    app.post('/service/buy', function(req, res) {
        var id = parseInt(req.body.commodityId)
        var merchant = global.merchant
        var player = req.player
        console.log(id)
        if (isNaN(id)) {
            res.send({
                error: 1
            })
            return
        }
        //判断该商品id是否存在
        if (merchant[id]) {
            if (merchant[id].money && player.money > merchant[id].money) {
                //判断背包是否有格子
                if (player.bag.empty === 0) {
                    res.send({
                        error: 2
                    })
                    return
                }
                player.money -= merchant[id].money //扣钱
                player.savePlayer()
                player.bag.addItems([id]) //添加物品
                player.saveBag()
            } else {
                res.send({
                    error: 3
                })
                return
            }
        }
        res.send({
            error: 0
        })
    })

    //出售物品
    app.post('/service/sell', function(req, res) {
        var id = parseInt(req.body.commodityId)
        var number = parseInt(req.body.number)
        var player = req.player
        var item
        if (isNaN(id) || isNaN(number)) {
            res.send({
                error: 1
            })
            return
        }

        if (id < 1000000) {
            item = global.props.get(id)
        } else {
            item = player.itemInfo['' + id]
        }

        if (!item) {
            //物品不存在
            res.send({
                error: 2
            })
            return
        }

        var ret = player.bag.reduceItem(id, player)
        if (ret === 0) {
            var money = item.money * number
            player.money += money
            res.send({
                error: 0,
                money: money
            })
        } else {
            res.send({
                error: ret
            })
        }

    })

    //解印装备
    app.post('/service/relieve', function(req, res) {
            var id = parseInt(req.body.equipmentid)
            console.log(id)
            var player = req.player
                //判断背包是否有格子
            if (isNaN(id) || player.bag.empty === 0) {
                res.send({
                    error: 1
                })
                return
            }
            //判断装备是否存在
            var equipmentCount = player.bag.getItemCount(id)
            if (equipmentCount === 0) {
                //判断是否存在
                res.send({
                    error: 2
                })
                return
            }
            var equip = global.equipments.get(id)
            if (!equip.relieve) {
                //判断是否需要解印符
                res.send({
                    error: 3
                })
                return
            }
            //判断是否有解印符
            var itemCount = player.bag.getItemCount(equip.relieve[0])

            //判断数量是否够
            if (equip.relieve[1] > itemCount) {
                res.send({
                    error: 4
                })
                return
            }
            var item = global.props.get(id)


            var equipment = new Equipment(item)
                //初始化装备id
            equipment.id = utils.uuNumber()
                //初始化主属性
            var attri = equipment.attri
            var l = Math.floor(Math.random() * attri.length)
            attri = attri[l]
            for (var a in attri) {
                attri[a] = getRandomValue(attri[a][0], attri[a][1])
            }
            //副属性
            if (equipment.viceAttri) {
                var viceAttri = equipment.viceAttri
                var l = Math.floor(Math.random() * viceAttri.length)
                viceAttri = viceAttri[l]
                for (var a in viceAttri) {
                    viceAttri[a] = getRandomValue(viceAttri[a][0], viceAttri[a][1])
                }
            }
            //附加属性
            if (equipment.addAttri) {
                var addAttri = equipment.addAttri
                var l = Math.floor(Math.random() * addAttri.length)
                addAttri = addAttri[l]
                for (var a in addAttri) {
                    addAttri[a] = getRandomValue(addAttri[a][0], addAttri[a][1])
                }
            }
            player.itemInfo[equipment.id] = equipment
            player.bag.reduceItem(equip.relieve[0], equip.relieve[1])
            player.bag.reduceItem(id, 1)
            player.bag.addItem(equipment.id)
            player.saveBag()
            player.saveItemInfo()
            res.send({
                error: 0
            })
            return
        })
        //更换装备
    app.post('/service/equip', function(req, res) {
            var id = parseInt(req.body.equipmentid)
            var player = req.player
            if (isNaN(id) || player.bag.empty === 0) {
                res.send({
                    error: 1
                })
                return
            }
            //判断装备是否存在
            var equipmentCount = player.bag.getItemCount(id)

            if (equipmentCount === 0) {
                //判断是否存在
                res.send({
                    error: 2
                })
                return
            }
            equipment = player.itemInfo[id]
            if (equipment.type >= 30) {
                //不是装备
                res.send({
                    error: 3
                })
                return
            }
            //头部12头部13身体14脚15手16腰17项链18戒指21法杖22长刀23利爪24轻弓
            var replace = 0
            switch (equipment.type) {
                case 12:
                    replace = player.equipments.head
                    player.equipments.head = id
                    break
                case 13:
                    replace = player.equipments.body
                    player.equipments.body = id
                    break
                case 14:
                    replace = player.equipments.foot
                    player.equipments.foot = id
                    break
                case 15:
                    replace = player.equipments.hand
                    player.equipments.hand = id
                    break
                case 16:
                    replace = player.equipments.waist
                    player.equipments.waist = id
                    break
                case 17:
                    replace = player.equipments.necklace
                    player.equipments.necklace = id
                    break
                case 18:
                    replace = player.equipments.ring
                    player.equipments.ring = id
                    break
                default:
                    replace = player.equipments.weanpon
                    player.equipments.weanpon = id
                    break
            }
            if (replace != 0) {
                player.bag.addItem(replace)
            }
            player.bag.reduceItem(id)
            player.calcAttri()
            player.savePlayer()
            player.saveBag()
            res.send({
                error: 0
            })
            return
        })
        //卸下装备
    app.post('/service/unload', function(req, res) {
        var id = parseInt(req.body.part)
        var player = req.player
        if (isNaN(id) || player.bag.empty === 0) {
            res.send({
                error: 1
            })
            return
        }

        //头部12头部13身体14脚15手16腰17项链18戒指21法杖22长刀23利爪24轻弓
        var unload = 0
        switch (id) {
            case 1:
                unload = player.equipments.head
                player.equipments.head = 0
                break
            case 2:
                unload = player.equipments.body
                player.equipments.body = 0
                break
            case 3:
                unload = player.equipments.foot
                player.equipments.foot = 0
                break
            case 4:
                unload = player.equipments.hand
                player.equipments.hand = 0
                break
            case 5:
                unload = player.equipments.waist
                player.equipments.waist = 0
                break
            case 6:
                unload = player.equipments.necklace
                player.equipments.necklace = 0
                break
            case 7:
                unload = player.equipments.ring
                player.equipments.ring = 0
                break
            case 8:
                unload = player.equipments.weanpon
                player.equipments.weanpon = 0
                break
            default:
                res.send({
                    error: 2
                })
                return
                break
        }
        console.log('unload:' + unload)
        if (unload !== 0) {
            player.bag.addItem(unload)
            player.calcAttri()
            player.savePlayer()
            player.saveBag()
        }
        res.send({
            error: 0
        })
        return
    })
}

function getRandomValue(min, max) {
    var a1 = max - min
    var a2 = a1 + 1
    var a3 = Math.floor(Math.random() * a2) + Math.floor(Math.random() * a2)
    if (a3 > a1) {
        a3 = 2 * a1 - a3
    }
    return max - a3
}